﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentManage
{
    public partial class FrmEditStudent : Form
    {
        public FrmEditStudent()
        {
            InitializeComponent();
        }
        //1，构造函数传值
        private int stuId = 0;
        public FrmEditStudent(int _stuId)
        {
            InitializeComponent();
            stuId = _stuId;
        }

        //公共变量传值
        public int pubStuId;

        //定义委托
        private Action reLoad = null;

        private void FrmEditStudent_Load(object sender, EventArgs e)
        {
            InitClassInfo();//加载班级信息
            LoadStudentInfo();//加载学生信息

        }

        private void LoadStudentInfo()
        {
            //获取传过来的StuId
            // if (this.Tag != null && this.Tag.ToString() != "")
            if (this.Tag != null)
            {
                //如果传过来的值不为空，则赋值给stuId
                //int.TryParse(this.Tag.ToString(),out stuId);
                TagObject tagObject = (TagObject)this.Tag;
                //赋值
                stuId = tagObject.StuId;
                reLoad = tagObject.ReLoad;
            }
            string sql = "select StuId,StuName,ClassId,Sex,Phone from studentInfo where StuId=@StuId";
            MySqlParameter param = new MySqlParameter("@StuId",stuId);
            MySqlDataReader dr = SqlHelper.ExcuteReader(sql,param);
            //读取数据,只向前，不后退，读一条，丢一条
            if (dr.Read())
            {
                //赋值
                txtStuName.Text = dr["StuName"].ToString();
                txtPhone.Text = dr["Phone"].ToString();
                string sex = dr["sex"].ToString();                
                if (!string.IsNullOrEmpty(sex))
                {
                    if (sex == "男") rbtMale.Checked = true;
                    else rbtFemale.Checked = true;
                }
                int classId = (int)dr["ClassId"];
                cboClasses.SelectedValue = classId;
            }
        }

        private void InitClassInfo()
        {
            string sqlClass = "SELECT c.ClassId,g.GradeName,c.ClassName " +
                             "FROM gradeinfo g,classinfo c WHERE g.GradeId = c.GradeId";
            DataTable dtClassList = SqlHelper.GetDataTable(sqlClass);
            if (dtClassList.Rows.Count > 0)
            {
                foreach (DataRow row in dtClassList.Rows)
                {
                    string className = row["ClassName"].ToString();
                    string gradeName = row["GradeName"].ToString();
                    row["ClassName"] = className + "--" + gradeName;
                }
            }
            //设置数据源
            cboClasses.DataSource = dtClassList;
            cboClasses.DisplayMember = "ClassName";
            cboClasses.ValueMember = "ClassId";
            //默认选中第一行
            cboClasses.SelectedIndex = 0;
        }

        private void btnEditStu_Click(object sender, EventArgs e)
        {
            //获取页面输入信息
            string stuName = txtStuName.Text.Trim();
            int classId = (int)cboClasses.SelectedValue;
            string sex = rbtMale.Checked ? rbtMale.Text.Trim() : rbtFemale.Text.Trim();
            string phone = txtPhone.Text.Trim();
            //判断信息是否为空: 姓名+电话不可以为空
            if (string.IsNullOrEmpty(stuName))
            {
                MessageBox.Show("姓名不能为空", "修改学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(phone))
            {
                MessageBox.Show("电话不能为空", "修改学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //判断学生是否已经存在： 姓名+电话 
            string sqlExists = "SELECT COUNT(1) FROM studentinfo " +
                               "WHERE StuName=@StuName AND Phone=@Phone and StuId<>@StuId";
            MySqlParameter[] paraExists =
            {
                new MySqlParameter("@StuName",stuName),
                new MySqlParameter("@Phone",phone),
                 new MySqlParameter("@StuId",stuId)
            };
            object result = SqlHelper.ExecuteScalar(sqlExists, 1, paraExists);
            if ((long)result != 0)
            {
                MessageBox.Show("该学生已存在", "修改学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //不存在，添加入库操作
            string sqlEdit = "update studentInfo " +
                            "set StuName=@StuName,ClassId=@ClassId,Sex=@Sex,Phone=@Phone " +
                            "where StuId=@StuId";
            //参数设置
            MySqlParameter[] paraEdit =
            {
                    new MySqlParameter("@StuName",stuName),
                    new MySqlParameter("@ClassId",classId),
                    new MySqlParameter("@Sex",sex),
                    new MySqlParameter("@Phone",phone),
                    new MySqlParameter("@StuId",stuId)
                };
            //执行
            int rs = SqlHelper.ExecuteNonQuery(sqlEdit, paraEdit);
            if (rs > 0)
            {
                MessageBox.Show($"学生:{ stuName}修改成功", "修改学生信息提示提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //通过委托跳转页面，刷新数据 
                //委托：列表页面定义委托，把列表页加载数据列表的方法赋值给委托，同时传过去---修改页面
                //修改页面定义委托，把传过来的委托赋值给本页定义的委托，修改成功后，调用委托
                reLoad.Invoke();
            }
            else
            {
                MessageBox.Show("学生修改失败", "修改学生信息提示提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
